package com.itcast.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itcast.yygh.hosp.repository.DepartmentRepository;
import com.itcast.yygh.hosp.service.DepartmentService;
import com.itcast.yygh.model.model.hosp.Department;
import com.itcast.yygh.model.vo.hosp.DepartmentQueryVo;
import com.itcast.yygh.model.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    //引入依赖
    @Resource
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> map) {
        //1.获取map中的科室对象
        Department department = JSONObject.parseObject(JSONObject.toJSONString(map), Department.class);
        //2.根据科室中的医院编码和部门编码查询mongo
        Department oldDepartment = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        //3.如果查询到的对象有值就执行更新操作否则就执行添加操作
        if(Objects.isNull(oldDepartment)){
            //执行保存操作
            //创建时间
            department.setCreateTime(new Date());
            //更新时间
            department.setUpdateTime(new Date());
            //是否删除
            department.setIsDeleted(0);
            //执行添加操作
            departmentRepository.save(department);
        }else{
            //执行更新操作
            //id
            department.setId(oldDepartment.getId());
            //设置创建时间
            department.setCreateTime(oldDepartment.getCreateTime());
            //更新时间
            department.setUpdateTime(new Date());
            //是否删除
            department.setIsDeleted(oldDepartment.getIsDeleted());
            //执行更新操作
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //1.根据字段进行排序
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //2.设置分页需要条件  当前页 每页显示的条数  排序规则
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        //3.设置查询条件
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        //4.创建查询条件的实例和条件的匹配器
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true);

        Example<Department> example = Example.of(department,matcher);
        //5.执行分页查询的方法
        Page<Department> pages = (Page<Department>) departmentRepository.findAll(example,pageable);
        return pages;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        //1.先根据医院编号和科室编号查询数据库
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        //2.如果对象不为null就执行删除操作
        if(!Objects.isNull(department)){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> getDeptList(String hoscode) {
        //1.根据医院编号获取所有的科室数据
            //定义一个需要返回的集合对象
        List<DepartmentVo> departmentVos = new ArrayList<>();
        //根据hoscode查询科室数据
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        //查询库中的数据
        List<Department> departmentList = departmentRepository.findAll(example);
        //对科室数据进行数据分组
        Map<String, List<Department>> depMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //2.对科室数据进行处理将大部门的数据和小部门数据封装在一个科室对象中
        //遍历map集合中的数据将大部门的数据封装到集合对象中
        for (Map.Entry<String,List<Department>> entry:depMap.entrySet()) {
            //1.获取大科室的编号
            String bigcode = entry.getKey();
            //2.获取大科室对应的子部门的数据
            List<Department> departments = entry.getValue();
            //3.将内容封装到集合对象中
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(departments.get(0).getBigname());
            //4.将对应的子部门的信息封装到结果集的子集合中
            List<DepartmentVo> childrenList = new ArrayList<>();
            //5.需要循环子部门的集合将子部门的数据封装的children
            for (Department depart:departments) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                //1.子部门的编号
                departmentVo1.setDepcode(depart.getDepcode());
                //2.子部门的名称
                departmentVo1.setDepname(depart.getDepname());
                //3.将子部门封装子部门集合中
                childrenList.add(departmentVo1);
            }
            //子部门列表的数据存放在父部门对象中
            departmentVo.setChildren(childrenList);
            //把父部门的数据存放在返回的集合中
            departmentVos.add(departmentVo);
        }
        //4.返回最终处理的数据
        return departmentVos;
    }

    @Override
    public String getDeptName(String hoscode, String depcode) {
        //查询单个科室信息
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if(Objects.isNull(department)){
            return "";
        }
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
    }
}
